package com.coocaa.ops.admin.v1.audit.provider.impl;


import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.v1.audit.model.CheckInfoModel;
import com.coocaa.ops.admin.v1.audit.provider.AbstractAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.poster.biz.impl.GlobalPictureBizImpl;
import com.coocaa.ops.admin.v1.tab.biz.impl.TabInfoBizImpl;
import com.coocaa.ops.admin.v1.tab.service.impl.TabInfoQueryServiceImpl;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.panel.entity.PanelInfoEntity;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import com.coocaa.ops.common.core.biz.tab.constant.TabTypeConstants;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import com.coocaa.ops.common.core.biz.tab.entity.TabPanelConfigInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author bijiahao
 * @date : 2018/7/12.
 * @description: 审核前校验-审核-撤销审核-上架操作 提供者接口实现-版面
 */
@Service
public class TabAuditTaskProviderImpl extends AbstractAuditTaskProviderImpl<TabInfoEntity> {
    @Autowired
    private TabInfoBizImpl tabInfoBiz;
    @Autowired
    private TabInfoQueryServiceImpl tabInfoQueryService;
    @Autowired
    private GlobalPictureBizImpl pictureBiz;

    @Override
    public AbstractBizImpl getAbstractBizImpl() {
        return tabInfoBiz;
    }

    /**
     * 审核前的校验
     *
     * @param resourceBaseModel
     * @return
     */
    @Override
    public CheckInfoModel checkBeforeAudit(ResourceBaseModel resourceBaseModel) {
        TabInfoEntity entity = tabInfoQueryService.getDetailInfo(resourceBaseModel.getId(), resourceBaseModel.getVersion());
        //背景图校验
        CheckInfoModel checkInfoModel = validBg(entity);
        if (checkInfoModel.isResult()) {
            return checkInfoModel;
        }
        //对板块校验
        if (TabTypeConstants.isNormal(entity.getTabType())) {
            List<TabPanelConfigInfo> panelList = entity.getPanelList();
            for (TabPanelConfigInfo panelInfo : panelList) {
                //检查定向精细化板块
                if (Objects.equals(1, panelInfo.getIsDmpPanel())) {
                    checkInfoModel = this.validPanelList(panelInfo.getDmpPanelList());
                    if (checkInfoModel.isResult()) {
                        return checkInfoModel;
                    }
                } else {
                    if (ResourceStatusValidator.isNotOnline(panelInfo.getStatus())) {
                        //存在没有审核通过的板块
                        return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(panelInfo));
                    }
                }
            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }


    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private CheckInfoModel validBg(TabInfoEntity tabInfoEntity) {
        String bgUrl = tabInfoEntity.getBackgroundPoster();
        GlobalPictureEntity pictureEntity = checkPosterByUrl(bgUrl);
        if (null != pictureEntity && ResourceStatusValidator.isNotOnline(pictureEntity.getStatus())) {
            return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(pictureEntity));
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }

    private CheckInfoModel validPanelList(List<TabPanelConfigInfo> panelList) {
        if (CollectionUtils.isEmpty(panelList)) {
            return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
        }
        for (TabPanelConfigInfo tabPanelConfigInfo : panelList) {
            if (ResourceStatusValidator.isNotOnline(tabPanelConfigInfo.getStatus())) {
                //存在没有审核通过的定向板块
                return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(tabPanelConfigInfo));
            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }


    private GlobalPictureEntity checkPosterByUrl(String url) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        QueryTemplate<GlobalPictureEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(GlobalPictureEntity::getPictureUrl, url);
        List<GlobalPictureEntity> list = pictureBiz.getMapper().selectList(queryTemplate);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }


    /**
     * 获取校验信息-板块
     *
     * @param panelInfo
     * @return
     */
    private String getCheckMsg(PanelInfoEntity panelInfo) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("panelID: [")
                .append(panelInfo.getId())
                .append("],")
                .append("panelName： ")
                .append(panelInfo.getPanelName())
        ;
        return stringBuilder.toString();
    }

    private String getCheckMsg(GlobalPictureEntity entity) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("posterID: [")
                .append(entity.getId())
                .append("],")
                .append("posterName： ")
                .append(entity.getPictureName())
        ;
        return stringBuilder.toString();
    }
}
